package com.nhim.cms.admin.client;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.HasWidgets;
import com.nhim.cms.admin.client.AdministrationTree.Listener;
import com.nhim.cms.admin.client.ContentPagesTree.SelectionChangeListener;
import com.nhim.cms.admin.client.eldf.ui.ElementDefinition;
import com.nhim.cms.admin.client.eldf.ui.ElementDefinitionList;
import com.nhim.cms.admin.client.place.ContentManagementPlace;
import com.nhim.cms.admin.client.place.CustomWidgetListPlace;
import com.nhim.cms.admin.client.place.DashboardPlace;
import com.nhim.cms.admin.client.place.ElementDefinitionListPlace;
import com.nhim.cms.admin.client.place.ElementDefinitionPlace;
import com.nhim.cms.admin.client.place.ContentTypePlace;
import com.nhim.cms.admin.client.place.ContentTemplateListPlace;
import com.nhim.cms.admin.shared.ElementDefinitionProxy;
import com.nhim.cms.admin.shared.SimpleTreeItem;

public class NhimCMSAdminApp {

	private static final Logger log = Logger.getLogger(NhimCMSAdminShell.class
			.getName());
	private final ActivityManager activityManager;
	private final EventBus eventBus;
	private final PlaceController placeController;
	private final PlaceHistoryHandler placeHistoryHandler;
	private final NhimCMSAdminShell shell;
	private final AdministrationTree administrationTree;
	private final ContentPagesTree contentPagesTree;
	private final AssetsTree assetsTree;
	public static final String[] ADMINISTRATIONMENU = {
			"System Administration", "Global Settings", "Templates", };

	/**
	 * The text color of the selected tab.
	 */
	private static final String SELECTED_TAB_COLOR = "#FDA05B";

	public NhimCMSAdminApp(ActivityManager activityManager, EventBus eventBus,
			PlaceController placeController,
			PlaceHistoryHandler placeHistoryHandler,
			DashboardsTree dashboardsTree,
			AdministrationTree administrationTree,
			ContentPagesTree contentPagesTree, AssetsTree assetsTree,
			NhimCMSAdminShell shell) {
		this.activityManager = activityManager;
		this.eventBus = eventBus;
		this.placeController = placeController;
		this.placeHistoryHandler = placeHistoryHandler;
		this.administrationTree = administrationTree;
		this.contentPagesTree = contentPagesTree;
		this.assetsTree = assetsTree;
		this.shell = shell;
	}

	/**
	 * Start the app, and add its main widget to the given panel.
	 */
	public void run(HasWidgets root) {
		GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
			public void onUncaughtException(Throwable e) {
				log.log(Level.SEVERE, e.getMessage(), e);
			}
		});
		final ElementDefinitionList elementDefinitionList = shell
				.getElementDefinitionList();
		final ElementDefinition elementDefinition = shell
				.getElementDefinition();
		elementDefinitionList.setListener(new ElementDefinitionList.Listener() {
			public void onElementDefinitionSelected(
					ElementDefinitionProxy elementDefinition) {
				placeController.goTo(new ElementDefinitionPlace(
						elementDefinition.stableId()));
			}
		});
		elementDefinition.setListener(new ElementDefinition.Listener() {

			public void onDeleted() {
				placeController.goTo(new ElementDefinitionListPlace("edf"));

			}
		});

		shell.administrationButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				shell.treeMenu.add(administrationTree);
				shell.contentPagesButton.getElement().getStyle().clearColor();
				shell.administrationButton.getElement().getStyle()
						.setColor(SELECTED_TAB_COLOR);
				shell.assetsButton.getElement().getStyle().clearColor();

			}
		});
		shell.contentPagesButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				shell.treeMenu.add(contentPagesTree);
				shell.contentPagesButton.getElement().getStyle()
						.setColor(SELECTED_TAB_COLOR);
				shell.administrationButton.getElement().getStyle().clearColor();
				shell.assetsButton.getElement().getStyle().clearColor();
				placeController.goTo(new ContentManagementPlace("ctmp!init"));

			}
		});
		shell.assetsButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				shell.treeMenu.add(assetsTree);
				shell.contentPagesButton.getElement().getStyle().clearColor();
				shell.administrationButton.getElement().getStyle().clearColor();
				shell.assetsButton.getElement().getStyle()
						.setColor(SELECTED_TAB_COLOR);

			}
		});
		administrationTree.setListener(new Listener() {

			public void onSelection(MenuItem item) {
				if (item != null) {
					String displayName = item.getDisplayName();
					switch (item.getCategory()) {
					case GLOBAL_SETTINGS:

						if (displayName.equalsIgnoreCase("Element Definition")) {

							placeController.goTo(new ElementDefinitionListPlace("edf"));
							return;

						}
						if (displayName.equalsIgnoreCase("Custom Widget")) {

							placeController.goTo(new CustomWidgetListPlace("cwg"));
							return;

						}
						if (displayName.equalsIgnoreCase("Content Type")) {

							placeController.goTo(new ContentTypePlace("ContentType"));
							return;

						}
						break;
					case TEMPLATE:
						placeController.goTo(new ContentTemplateListPlace("ContentTemplateList!" + item.getId()));
						break;
					case SYSTEM_ADMINISTRATION:
						//placeController.goTo(new GroupListPlace("grlp"));
						break;
					}

				}
			}
		});

		/*************** Region ElementDefinition Event Handler ***********************/
		
		elementDefinition.getCancelLink().addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				placeController.goTo(new ElementDefinitionListPlace("edf"));

			}
		});
		elementDefinitionList.GetCreateNewLink().addClickHandler(
				new ClickHandler() {

					public void onClick(ClickEvent event) {
						placeController.goTo(new ElementDefinitionPlace(null));

					}
				});
		
		/*************** Region Content Management Event Handler ***********************/
		
		contentPagesTree.onSelectionChange(new SelectionChangeListener(){

			@Override
			public void onTreeItemSelected(SimpleTreeItem selectedItem) {
				placeController.goTo(new ContentManagementPlace(selectedItem.getName()));				
			}});
		
		
		
		// Give the ActivityManager a panel to run
		activityManager.setDisplay(shell.getPanel());

		// Browser history integration
		placeHistoryHandler.register(placeController, eventBus,
				new DashboardPlace("D"));
		placeHistoryHandler.handleCurrentHistory();
		root.add(shell);
	}
}
